//
//  OSEssenceViewController.m
//  百思不得姐
//
//  Created by gl on 16/2/19.
//  Copyright © 2016年 CoderC. All rights reserved.
//

#import "OSEssenceViewController.h"
#import "OSRecommendTagsViewController.h"
#import "OSTopicViewController.h"

@interface OSEssenceViewController ()<UIScrollViewDelegate>


@property (nonatomic,weak)UIButton *selectButton;

@property (nonatomic,weak)UIView *indicatorView;

@property (nonatomic,weak)UIView *titleView;

@property (nonatomic,weak)UIScrollView *contentView;

@end

@implementation OSEssenceViewController

-(void)viewDidLoad{
    [super viewDidLoad];
    
    [self setupNavButton];
    
    [self setupChildVc];
    
    [self setupTitleView];
    
    [self setupContentView];
    

}
-(void)setupChildVc{
    
    
    OSTopicViewController *all = [[OSTopicViewController alloc]init];
    all.title = @"全部";
    all.type = OSTopicTypeAll;
    [self addChildViewController:all];
    
    
    OSTopicViewController *picture = [[OSTopicViewController alloc]init];
    picture.title = @"图片";
    picture.type = OSTopicTypePicture;
    [self addChildViewController:picture];
    
    
    OSTopicViewController *video = [[OSTopicViewController alloc]init];
    video.title = @"视频";
    video.type = OSTopicTypeVideo;
    [self addChildViewController:video];
    
    
    
    OSTopicViewController *voice = [[OSTopicViewController alloc]init];
    voice.title = @"声音";
    voice.type = OSTopicTypeVoice;
    [self addChildViewController:voice];
    
    
    
    OSTopicViewController *word = [[OSTopicViewController alloc]init];
    word.title = @"段子";
    word.type = OSTopicTypeWord;
    [self addChildViewController:word];
    
    
   
}

-(void)setupContentView{
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    UIScrollView *contentView = [[UIScrollView alloc]init];
    contentView.frame = self.view.bounds;
    contentView.delegate = self;
    contentView.pagingEnabled = YES;
    
    [self.view insertSubview:contentView atIndex:0];
    contentView.contentSize = CGSizeMake(self.view.width * self.childViewControllers.count , 0);
    self.contentView = contentView;
    
    [self scrollViewDidEndScrollingAnimation:contentView];

}
-(void)setupTitleView{
    // 标签栏整体
    UIView *titlesView = [[UIView alloc] init];
    titlesView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.7];
    titlesView.width = self.view.width;
    titlesView.height = OSTitilesViewH;
    titlesView.y = OSTitilesViewY;
    [self.view addSubview:titlesView];
    self.titleView = titlesView;
    
    // 底部的红色指示器
    UIView *indicatorView = [[UIView alloc] init];
    indicatorView.backgroundColor = [UIColor redColor];
    indicatorView.height = 2;
    indicatorView.tag = -1;
    indicatorView.y = titlesView.height - indicatorView.height;
    self.indicatorView = indicatorView;
    
    // 内部的子标签
    CGFloat width = titlesView.width / self.childViewControllers.count;
    CGFloat height = titlesView.height;
    for (NSInteger i = 0; i<self.childViewControllers.count; i++) {
        UIButton *button = [[UIButton alloc] init];
        button.tag = i;
        button.height = height;
        button.width = width;
        button.x = i * width;
        UIViewController *vc = self.childViewControllers[i];
        [button setTitle:vc.title forState:UIControlStateNormal];
        //        [button layoutIfNeeded]; // 强制布局(强制更新子控件的frame)
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor redColor] forState:UIControlStateDisabled];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        [button addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        [titlesView addSubview:button];
        
        // 默认点击了第一个按钮
        if (i == 0) {
            button.enabled = NO;
            self.selectButton = button;
            
            // 让按钮内部的label根据文字内容来计算尺寸
            [button.titleLabel sizeToFit];
            self.indicatorView.width = button.titleLabel.width;
            self.indicatorView.centerX = button.centerX;
        }
    }
    
    [titlesView addSubview:indicatorView];
}
-(void)titleClick:(UIButton*)button{
    
    self.selectButton.enabled = YES;
    button.enabled = NO;
    self.selectButton = button;
    [UIView animateWithDuration:0.25 animations:^{
        self.indicatorView.width = button.titleLabel.width;
        self.indicatorView.centerX = button.centerX;
    }];
    
    CGPoint offset = self.contentView.contentOffset;
    offset.x = button.tag * self.contentView.width;
    [self.contentView setContentOffset:offset animated:YES ];
    

}
-(void)setupNavButton{
    
    self.view.backgroundColor = OSGlobalBg;
    
    self.navigationItem.titleView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"MainTitle"]];
    
    
    UIBarButtonItem *leftButton = [UIBarButtonItem itemWithImageName:@"MainTagSubIcon" highImageName:@"MainTagSubIconClick" target:self action:@selector(buttonClick)];
    
    self.navigationItem.leftBarButtonItem = leftButton;
    
}
-(void)buttonClick{
    
    OSRecommendTagsViewController *vcc = [[OSRecommendTagsViewController alloc]init];
    [self.navigationController pushViewController:vcc animated:YES];
    
    
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    // 当前的索引
    NSInteger index = scrollView.contentOffset.x / scrollView.width;
    
    // 取出子控制器
    UIViewController *vc = self.childViewControllers[index];
    vc.view.x = scrollView.contentOffset.x;
    vc.view.y = 0; // 设置控制器view的y值为0(默认是20)
    vc.view.height = scrollView.height; // 设置控制器view的height值为整个屏幕的高度(默认是比屏幕高度少个20)
    [scrollView addSubview:vc.view];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self scrollViewDidEndScrollingAnimation:scrollView];
    
    // 点击按钮
    NSInteger index = scrollView.contentOffset.x / scrollView.width;
    [self titleClick:self.titleView.subviews[index]];
}
@end
